//
//  RGSeachVedioListViewController.m
//  RedGrass
//
//  Created by wukong on 2019/2/13.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGSeachVedioListViewController.h"
#import "RGSearchVedioView.h"
#import "HttpRequest.h"
#import "RGRecommendVideoModel.h"
#import "RGVediosDetailViewController.h"


#define pageSize  10

@interface RGSeachVedioListViewController ()<RGSearchVedioViewDelegate>

@property (strong, nonatomic) RGSearchVedioView *searchVedioView;
@property (strong, nonatomic) NSMutableArray *vedioList;
@property (assign, nonatomic) NSInteger pageNum;
@property (strong, nonatomic) NSString *content;


@end

@implementation RGSeachVedioListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self showBackButton];
    _pageNum = 1;
    self.NavStyleType = RGCustomNavStyleType_default;
    self.customNavBar.hidden = YES;
    [self.customContentView addSubview:self.searchVedioView];
    [_searchVedioView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

#pragma mark - httpQuest

-(void) searchVedioWithContent:(NSString *)content isLoadMore:(BOOL)isloadMore{
    NSDictionary *dic;
    NSString *token = [RGUserInfoModel getUserInfo].token;
    if(isloadMore){
        _pageNum ++;
        dic = @{@"token":SafeStr(token),
                @"content":SafeStr(content),
                @"type":@(1),
                @"pageSize":@(pageSize),
                @"pageNumber":@(_pageNum)
                };
    }else{
        _pageNum = 1;
        _content = content;
        dic = @{@"token":SafeStr(token),
                @"content":SafeStr(content),
                @"type":@(1),
                @"pageSize":@(pageSize),
                @"pageNumber":@(_pageNum)
                };
    }
    AA_WEAK_MAKE(self) weakSelf = self;
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/searchResult"];
    [quest post:dic success:^(id data) {
//        NSLog(@"rrr");
        NSInteger total = [[data objectForKey:@"total"] integerValue];
        NSArray *list = [data objectForKey:@"list"];
        NSMutableArray *tempArr = [NSMutableArray array];
        NSError *error;
        if([list isKindOfClass:[NSArray class]] && list.count){
            tempArr = [RGRecommendVideoModel arrayOfModelsFromDictionaries:list error:&error];
        }
        if(isloadMore){
        
            if([weakSelf.vedioList count]){
                if(tempArr.count){
                    [weakSelf.vedioList addObjectsFromArray:tempArr];
                }
            }else{
                weakSelf.vedioList = tempArr;
            }
            [weakSelf.searchVedioView endFooterFresh];
        }else{
            weakSelf.vedioList = tempArr;
        }
       
        if(list.count < pageSize){
            if(isloadMore){
                [weakSelf.searchVedioView viewHaveNoMoreData];
            }else{
                [weakSelf.searchVedioView endFooterFresh];
            }
        }else{
            [weakSelf.searchVedioView endFooterFresh];
        }
        [weakSelf.searchVedioView reloadDataWithVedioList:weakSelf.vedioList];
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf showTextMessageWithMessage:message];
        if(isloadMore){
            [weakSelf.searchVedioView endFooterFresh];
        }
    }];
}

#pragma mark - viewDelegate
-(void)loadMoreDataWithView:(RGSearchVedioView *)view{
    [self searchVedioWithContent:SafeStr(_content) isLoadMore:YES];
}

-(void) cellItemChosedWithView:(RGSearchVedioView *)view chosedIndexPath:(NSIndexPath *)path{
    RGVediosDetailViewController *vc = [[RGVediosDetailViewController alloc]init];
    [vc initViewWithVedioList:_vedioList content:_content index:path.row];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - getter

-(RGSearchVedioView *)searchVedioView{
    if(!_searchVedioView){
        _searchVedioView = [[RGSearchVedioView alloc]init];
        _searchVedioView.viewDelegate = self;
    }
    return _searchVedioView;
}


@end
